home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Camelot / Camelot 059 (1989-12)(Swedish User Group of Amiga)(SE)(PD)[WB].zip / Camelot 059 (1989-12)(Swedish User Group of Amiga)(SE)(PD)[WB].adf / Utils / getoptx.c < prev    next >
C/C++ Source or Header  |  1989-11-14  |  4KB  |  114 lines

  1. /*
  2. **      @(#)getopt.c    2.5 (smail) 9/15/87
  3. */
  4.  
  5. /*
  6.  *  NOTE: this source has been hacked to allow * in the optstring,
  7.  *  meaning "an optional argument which must follow the option
  8.  *  character without a blank." Read the comments following with
  9.  *  that in mind.
  10.  *    bill davidsen (davidsen@crdos1.crd.ge.com) June 1989
  11.  */
  12.  
  13. #define OptArgFlg '*'
  14.  
  15. /*
  16.  * Here's something you've all been waiting for:  the AT&T public domain
  17.  * source for getopt(3).  It is the code which was given out at the 1985
  18.  * UNIFORUM conference in Dallas.  I obtained it by electronic mail
  19.  * directly from AT&T.  The people there assure me that it is indeed
  20.  * in the public domain.
  21.  *
  22.  * There is no manual page.  That is because the one they gave out at
  23.  * UNIFORUM was slightly different from the current System V Release 2
  24.  * manual page.  The difference apparently involved a note about the
  25.  * famous rules 5 and 6, recommending using white space between an option
  26.  * and its first argument, and not grouping options that have arguments.
  27.  * Getopt itself is currently lenient about both of these things White
  28.  * space is allowed, but not mandatory, and the last option in a group can
  29.  * have an argument.  That particular version of the man page evidently
  30.  * has no official existence, and my source at AT&T did not send a copy.
  31.  * The current SVR2 man page reflects the actual behavor of this getopt.
  32.  * However, I am not about to post a copy of anything licensed by AT&T.
  33.  */
  34.  
  35. /* for SysV use strchr instead of index */
  36. #if defined(USG) || defined(AMIGA)
  37. #define index   strchr
  38. #define rindex strrchr
  39. #endif
  40.  
  41. /*LINTLIBRARY*/
  42. #define NULL    0
  43. #define EOF     (-1)
  44. #define ERR(s, c)      if(opterr){\
  45.         extern int write();\
  46.         char errbuf[2];\
  47.         errbuf[0] = c; errbuf[1] = '\n';\
  48.         (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
  49.         (void) write(2, s, (unsigned)strlen(s));\
  50.         (void) write(2, errbuf, 2);}
  51. .
  52. extern char *index();
  53.  
  54. int     opterr = 1;
  55. int     optind = 1;
  56. int     optopt;
  57. char    *optarg;
  58.  
  59. int
  60. getoptx(argc, argv, opts)
  61. int     argc;
  62. char    **argv, *opts;
  63. {
  64.         static int sp = 1;
  65.         register int c, ch;
  66.         register char *cp;
  67.  
  68.         if(sp == 1)
  69.                 if(optind >= argc ||
  70.                    argv[optind][0] != '-' || argv[optind][1] == '\0')
  71.                         return(EOF);
  72.                 else if(strcmp(argv[optind], "--") == NULL) {
  73.                         optind++;
  74.                         return(EOF);
  75.                 }
  76. .
  77.         optopt = c = argv[optind][sp];
  78.         if(c == ':' || c == OptArgFlg || (cp=index(opts, c)) == NULL) {
  79.                 ERR(": illegal option -- ", c);
  80.                 if(argv[optind][++sp] == '\0') {
  81.                         optind++;
  82.                         sp = 1;
  83.                 }
  84.                 return('?');
  85.         }
  86.         if((ch = *++cp) == OptArgFlg) {
  87.                 if(argv[optind][sp+1] != '\0')
  88.                         optarg = &argv[optind++][sp+1];
  89.                 else {
  90.                         optarg = "";
  91.                         optind++;
  92.                         sp = 1;
  93.                 }
  94.         }
  95.         else if(ch == ':') {
  96.                 if(argv[optind][sp+1] != '\0')
  97.                         optarg = &argv[optind++][sp+1];
  98.                 else if(++optind >= argc) {
  99.                         ERR(": option requires an argument -- ", c);
  100.                         sp = 1;
  101.                         return('?');
  102.                 } else
  103.                         optarg = argv[optind++];
  104.                 sp = 1;
  105.         } else {
  106.                 if(argv[optind][++sp] == '\0') {
  107.                         sp = 1;
  108.                         optind++;
  109.                 }
  110.                 optarg = NULL;
  111.         }
  112.         return(c);
  113. }
  114.